using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NBusiness.Templates.Sql
{
    public partial class SqlScriptsConnectionWindow : Form
    {
		private SqlScriptsOptions _options = null;
        public SqlScriptsOptions Options
        {
            get { return this._options; }
        }

        public SqlScriptsConnectionWindow()
        {
            InitializeComponent();
        }
        public SqlScriptsConnectionWindow(SqlScriptsOptions options)
		{
			InitializeComponent();

			this.textServer.Text = options.ServerName;
			this.textDatabaseName.Text = options.DatabaseName;
			this.textFilePath.Text = options.DatabaseFilePath;
			this.textPassword.Text = options.SqlServerPassword;
			this.textUsername.Text = options.SqlServerUsername;

			this.radioDatabaseFile.Checked = options.UseDatabaseFile;
			this.radioSqlServer.Checked = !options.UseDatabaseFile;
			this.radioWindowsAuth.Checked = options.IntegratedSecurity;
			this.radioSqlAuth.Checked = !options.IntegratedSecurity;
            _options = options;
		}

		private void buttonTestConnection_Click(object sender, EventArgs e)
		{
			try
			{
                string connectionstring = _options.GetConnectionString();
                using (SqlConnection conn = new SqlConnection(connectionstring))
                {
                    conn.Open();
                    if (conn.State == ConnectionState.Open)
                    {
                        MessageBox.Show(this,
                            "Connection Successful",
                            "Connected",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                    }
                    else throw new Exception("Error Connecting to Database!");
                }
			}
			catch (Exception ex)
			{
				MessageBox.Show(this,
					ex.Message,
					"Error Connecting!",
					MessageBoxButtons.OK,
					MessageBoxIcon.Error);
			}
		}

		private void buttonDone_Click(object sender, EventArgs e)
		{
            _options.ServerName = this.textServer.Text;
            _options.DatabaseName = this.textDatabaseName.Text;
            _options.DatabaseFilePath = this.textFilePath.Text;
            _options.SqlServerPassword = this.textPassword.Text;
            _options.SqlServerUsername = this.textUsername.Text;

            _options.UseDatabaseFile = this.radioDatabaseFile.Checked;
            _options.IntegratedSecurity = this.radioWindowsAuth.Checked;
            _options.UseDatabaseFile = !this.radioSqlServer.Checked;
            _options.IntegratedSecurity = !this.radioSqlAuth.Checked;
		}

		private void radioDatabaseFile_CheckedChanged(object sender, EventArgs e)
		{
			textFilePath.Enabled = radioDatabaseFile.Checked;
			buttonBrowse.Enabled = radioDatabaseFile.Checked;
			textDatabaseName.Enabled = !radioDatabaseFile.Checked;
			textServer.Enabled = !radioDatabaseFile.Checked;
		}

		private void radioSqlAuth_CheckedChanged(object sender, EventArgs e)
		{
			this.textPassword.Enabled = this.radioSqlAuth.Checked;
			this.textUsername.Enabled = this.radioSqlAuth.Checked;
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
			{
				this.textFilePath.Text = this.openFileDialog1.FileName;
			}
		}
    }
}